/**
 * @file smt_Interrupt.c
 * @author mjsz (1349513606@qq.com)
 * @brief  不进电机中断服务处理
 * @version 0.1
 * @date 2025-05-23
 *
 * @copyright Copyright (c) 2025
 *
 */
#include "smt.h"
#include "gpio_pin.h"
static inline void _smt_moving_update_it_handle(smt_t smt_obj)
{

    switch (smt_obj->curve_state)
    {
    case SMT_STATE_ACCELERATE:
        smt_obj->vel_curve.acc_steps--;
        if (smt_obj->vel_curve.acc_steps > 0) // 未到达加速终点
        {
            __HAL_TIM_SET_PRESCALER(smt_obj->hw->htim, smt_obj->vel_curve.dec_tab[smt_obj->vel_curve.acc_steps]);
        }
        else if (smt_obj->cur_pos <= 0) // 到达加速终点
        {
            smt_obj->curve_state = SMT_STATE_UNIFORM;
        }
        break;
    case SMT_STATE_UNIFORM:
        smt_obj->vel_curve.uniform_steps--;
        if (smt_obj->vel_curve.uniform_steps <= 0) // 到达匀速终点
        {
            __HAL_TIM_SET_PRESCALER(smt_obj->hw->htim, smt_obj->vel_curve.acc_tab[smt_obj->vel_curve.dec_steps]);
            smt_obj->curve_state = SMT_STATE_DECELERATE; // 进入减速阶段
        }
        break;
    case SMT_STATE_DECELERATE:
        smt_obj->vel_curve.dec_steps--;
        if (smt_obj->vel_curve.dec_steps > 0) // 未到达减速终点
        {
            __HAL_TIM_SET_PRESCALER(smt_obj->hw->htim, smt_obj->vel_curve.dec_tab[smt_obj->vel_curve.dec_steps]);
        }
        else if (smt_obj->vel_curve.dec_steps <= 0) // 到达减速终点
        {
            __HAL_TIM_DISABLE_IT(smt_obj->hw->htim, TIM_IT_UPDATE | smt_obj->hw->pwm_chx_itr); // 关闭中断
            __HAL_TIM_DISABLE(smt_obj->hw->htim);                                             // 关闭timer PWM输出
            smt_obj->curve_state = SMT_STATE_STOPPED;                                      // 停止电机
            if (smt_obj->evt_handler != NULL)
            {
                smt_obj->evt_handler(smt_obj, SMT_EVT_MOTION_DONE); // 触发停止事件
            }
            smt_obj->curve_state = SMT_STATE_IDLE; // 重置状态为空闲
        }
        break;
    default:
        break;
    }
}

static inline void _smt_back_origin_update_it_handle(smt_t smt_obj)
{
    if ((smt_obj->origin_state == SMT_ORIGIN_FINGING) &&
        (pin_get(smt_obj->hw->limit_pin) == smt_obj->hw->limit_lvl)) // 找到原点
    {
        switch (smt_obj->curve_state)
        {
        case SMT_STATE_ACCELERATE:
            smt_obj->vel_curve.acc_steps--;   // 加速速步数减一
            smt_obj->vel_curve.uniform_steps = 0; // 匀速步数设置为0
            smt_obj->vel_curve.dec_steps -= smt_obj->vel_curve.acc_steps;
            __HAL_TIM_SET_PRESCALER(smt_obj->hw->htim, smt_obj->vel_curve.dec_tab[smt_obj->vel_curve.dec_steps]);
            smt_obj->curve_state = SMT_STATE_DECELERATE; // 进入减速阶段
            break;
        case SMT_STATE_UNIFORM:
            smt_obj->vel_curve.uniform_steps = 0; // 匀速步数设置为0
            __HAL_TIM_SET_PRESCALER(smt_obj->hw->htim, smt_obj->vel_curve.dec_tab[smt_obj->vel_curve.dec_steps]);
            smt_obj->curve_state = SMT_STATE_DECELERATE; // 进入减速阶段
            break;
        case SMT_STATE_DECELERATE:
            smt_obj->vel_curve.dec_steps--; // 减速步数减一
            __HAL_TIM_SET_PRESCALER(smt_obj->hw->htim, smt_obj->vel_curve.dec_tab[smt_obj->vel_curve.dec_steps]);

            break;
        default:
            break;
        }
        smt_obj->origin_state = SMT_ORIGIN_FINDED;
    }
    else
    {
        if (smt_obj->origin_state == SMT_ORIGIN_FINDED)
        {
            smt_obj->origin_offset--;
        }
        if (smt_obj->origin_offset <= 0)
        {
            smt_obj->cur_pos = 0;
            __HAL_TIM_DISABLE_IT(smt_obj->hw->htim, TIM_IT_UPDATE | smt_obj->hw->pwm_chx_itr); // 关闭中断
            __HAL_TIM_DISABLE(smt_obj->hw->htim);                                             // 关闭timer PWM输出
            smt_obj->curve_state = SMT_STATE_STOPPED;                                      // 停止电机
            if(smt_obj->evt_handler != NULL)
            {
                smt_obj->evt_handler(smt_obj, SMT_EVT_MOTION_DONE | SMT_EVT_ORIGIN_OK); // 触发找到原点事件
            }
            smt_obj->curve_state = SMT_STATE_IDLE;                                         // 重置状态为空闲

            return;
        }

        switch (smt_obj->curve_state)
        {
        case SMT_STATE_ACCELERATE:
            smt_obj->vel_curve.acc_steps--;

            if (smt_obj->vel_curve.acc_steps > 0) // 未到达加速终点
            {
                __HAL_TIM_SET_PRESCALER(smt_obj->hw->htim, smt_obj->vel_curve.dec_tab[smt_obj->vel_curve.acc_steps]);
            }
            else if (smt_obj->cur_pos <= 0) // 到达加速终点
            {
                smt_obj->curve_state = SMT_STATE_UNIFORM;
            }
            break;
        case SMT_STATE_UNIFORM:
            smt_obj->vel_curve.uniform_steps--;
            if (smt_obj->vel_curve.uniform_steps <= 0) // 到达匀速终点
            {
                if (smt_obj->origin_state != SMT_ORIGIN_FINDED) // 未找到原点，继续后退寻找原点
                {
                    __HAL_TIM_DISABLE_IT(smt_obj->hw->htim, TIM_IT_UPDATE | smt_obj->hw->pwm_chx_itr); // 关闭中断
                    __HAL_TIM_DISABLE(smt_obj->hw->htim);                                             // 关闭timer PWM输出
                    smt_obj->curve_state = SMT_STATE_STOPPED;                                      // 停止电机
                    if (smt_obj->evt_handler != NULL)
                    {
                        smt_obj->evt_handler(smt_obj, SMT_EVT_MOTION_DONE | SMT_EVT_NO_ORIGIN); // 触发未找到原点事件
                    }
                    smt_obj->curve_state = SMT_STATE_IDLE;                                         // 重置状态为空闲

                    return;
                }
                else
                {
                    __HAL_TIM_SET_PRESCALER(smt_obj->hw->htim, smt_obj->vel_curve.acc_tab[smt_obj->vel_curve.dec_steps]);
                    smt_obj->curve_state = SMT_STATE_ACCELERATE;
                }
            }
            break;
        case SMT_STATE_DECELERATE:
            smt_obj->vel_curve.dec_steps--;
            if (smt_obj->vel_curve.dec_steps > 0) // 未到达减速终点
            {
                __HAL_TIM_SET_PRESCALER(smt_obj->hw->htim, smt_obj->vel_curve.dec_tab[smt_obj->vel_curve.dec_steps]);
            }
            else if (smt_obj->vel_curve.dec_steps <= 0) // 到达减速终点
            {
                if (smt_obj->origin_state != SMT_ORIGIN_FINDED) // 未找到原点，继续后退寻找原点
                {
                    if (smt_obj->origin_offset > 0) // 还需后退寻找原点
                    {
                        smt_obj->vel_curve.uniform_steps = smt_obj->origin_offset;
                        smt_obj->curve_state = SMT_STATE_UNIFORM; // 进入匀速阶段
                    }
                }
            }
            break;
        default:
            break;
        }
    }
}

void smt_tim_update_it_handler(smt_t smt_obj)
{
    pin_set(smt_obj->hw->step_pin, 0); // 关闭步进脉冲
    if (smt_obj->dir == SMT_DIR_FORWARD)
    {
        smt_obj->cur_pos += 1; // 正转，当前位置加一
    }
    else
    {
        smt_obj->cur_pos -= 1; // 反转，当前位置减一
    }
    if (smt_obj->op_type == SMT_OP_ORIGIN)
    {
        _smt_back_origin_update_it_handle(smt_obj);
    }
    else
    {
        _smt_moving_update_it_handle(smt_obj);
    }
}

void smt_tim_pwm_chx_it_handler(smt_t smt_obj)
{
    pin_set(smt_obj->hw->step_pin, 1); // 产生步进脉冲
}
